﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Util;
using WAD.BLL;
using WAD.Client2.Ali;
using WAD.Client2.WXPay;


namespace WAD.WEB.Client2.Ali
{
    public partial class _return : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //读取站点配置信息
            SortedDictionary<string, string> sPara = GetRequestGet();
            if (sPara.Count > 0)//判断是否有带返回参数
            {
                bool verifyResult = AlipaySignature.RSACheckV1(sPara, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2", false);
                string errMsg = "";
                if (verifyResult)//验证成功
                {

                    //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                    //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表
                    //交易状态

                    string trade_no = sPara["trade_no"] ?? "";         //支付宝交易号
                    string order_no = sPara["out_trade_no"] ?? "";     //获取订单号(微街秀订单号)

                    var client = new DefaultAopClient(AlipayConfig.URL, AlipayConfig.APPID,
                 AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
                 AlipayConfig.SIGNTYPE, AlipayConfig.ALIPAY_PUBLIC_KEY);
                    var request = new AlipayTradeQueryRequest();
                    var model = new AlipayTradeQueryModel();
                    model.OutTradeNo = order_no;
                    model.TradeNo = trade_no;
                    request.SetBizModel(model);
                    var res = client.Execute(request);
                    if (res.Code == "10000" && res.TradeStatus == "TRADE_SUCCESS")
                    {
                        FCOrderBLL.Pay_CallBack(order_no, res.TotalAmount, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Request.UserHostAddress, GetMsg(trade_no, order_no, res.BuyerLogonId, res.TotalAmount, Request.UserHostAddress), WAD.WEB.AppCode.WJXConfig.EncryptConfig.DES_KEY);
                        //成功状态
                        //Response.Redirect("http://passport.711688.com.cn/Main.aspx");
                        //return;
                        Response.Clear();
                        Response.Write("<h1>付款成功，请关闭当前页面！</h1>");
                        Response.End();
                    }

                }

            }
            //失败状态
            Response.Clear();
            Response.Write("<h1>付款失败，请关闭当前页面！</h1>");
            Response.End();
            return;
        }
        /// <summary>
        /// 获取支付宝GET过来通知消息，并以“参数名=参数值”的形式组成数组
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public SortedDictionary<string, string> GetRequestGet()
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            coll = Request.QueryString;

            String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
            }

            return sArray;
        }
        string GetMsg(string trade_no, string order_no, string buyer_email, string total_fee, string IP)
        {
            return "{\"trade_no\":\"" + trade_no + "\",\"order_no\":\"" + order_no + "\",\"buyer_email\":\"" + buyer_email + "\",\"total_fee\":\"" + total_fee + "\",\"IP\":\"" + IP + "\"}";
        }
    }
}